package com.jiyun.courselist.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.courselist.feign.LabelFeignService;
import com.jiyun.courselist.mapper.QuizPaperMapper;
import com.jiyun.courselist.service.*;
import com.jiyun.model.*;
import com.jiyun.vo.CourseVo;
import com.jiyun.vo.ResultResopnse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    LableService lableService;

    @Autowired
    KuaijiService kuaijiService;

    @Autowired
    CaiwuService caiwuService;

    @Autowired
    QuizPaperService quizPaperService;

    @Autowired
    TrainQuizService trainQuizService;

    @Autowired
    LabelFeignService labelFeignService;

    @RequestMapping("findPage")
    public Page<CourseVo>findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody CourseVo courseVo){
         return courseService.findPage(pageNum,pageSize,courseVo);
    }

    @RequestMapping("getBaoMingId")
    public String setRedisDasaiId(@RequestParam Integer id){
       redisTemplate.opsForValue().set("BaoMingId",id+"");
       return "欢迎来到大赛报名页面";
    }

    @RequestMapping("dian")
    public void dian(@RequestParam Integer id){
        Course course = courseService.getById(id);
        course.setGood(course.getGood()+1);
        courseService.updateById(course);
        redisTemplate.delete(id+"");
        redisTemplate.boundValueOps(id+"").set(JSONObject.toJSONString(course));
    }

    @RequestMapping("biaoqian")
    public List<Label>biaoqian(){
        return lableService.list();
    }

    @RequestMapping("courseList")
    public List<Course> courseList(){
        return courseService.courseList();
    }

//    总题目
    @RequestMapping("totleti")
    public Integer totleti(@RequestParam Integer id){
       return courseService.totleti(id);
    }

//    已做题目
    @RequestMapping("finalti")
    public Integer finalti(@RequestParam Integer id){
        return courseService.finalti(id);
    }

//    课程目录之
//    第1章:会计导论
    @RequestMapping("findkuaiji")
    public List<Kuaiji>findkuaiji(){
        return kuaijiService.list();
    }

//    第2章:财务报表
    @RequestMapping("findCaiwu")
    public List<Caiwu>findCaiwu(){
        return caiwuService.list();
    }

//    添加
    @RequestMapping("tianjia")
    public void tianjia(@RequestParam Integer id){
        Kuaiji kuaiji = kuaijiService.getById(id);
        kuaiji.setState("已添加");
        kuaijiService.updateById(kuaiji);

        TrainQuiz quiz = trainQuizService.getById(kuaiji.getQid());
        quiz.setTid(1);
        trainQuizService.updateById(quiz);
    }

    @RequestMapping("tianjia2")
    public void tianjia2(@RequestParam Integer id){
        Caiwu caiwu = caiwuService.getById(id);
        caiwu.setState("已添加");
        caiwuService.updateById(caiwu);

        TrainQuiz quiz = trainQuizService.getById(caiwu.getTid());
        quiz.setTid(37);
        trainQuizService.updateById(quiz);
    }


//    标签的模糊查询
    @RequestMapping("findLabelAll")
    public ResultResopnse findLabelAll(){
        return labelFeignService.findAll();
    }

}
